<template>
  <div class="dashboard-container">
    <el-row :gutter="20">
      <el-col :span="3">
        <el-input v-model="title" placeholder="请输入标题" />
      </el-col>
      <el-button type="primary">查询</el-button>
      <el-button type="success">清除</el-button>
    </el-row>
    <el-button type="primary" style="margin-top:10px" @click="add()">发帖</el-button>
    <div v-for="item in data" :key="item.id" style="margin-top:10px" class="text item">
      <el-card style="margin-bottom:20px;position:relative">
        <el-tag type="info" style="width:40px;text-align:center">{{ item.id }}</el-tag>
        <el-tag v-if="item.isTop" effect="dark" style="margin-left:20px">置顶</el-tag>
        <el-tag effect="dark" type="danger" style="margin-left:20px">火爆</el-tag>
        <router-link :to="{path:'/communicate/communicate-detail',query:{id:item.id}}">
          <el-link style="margin-left:30px;font-size:16px">{{ item.title }}</el-link>
        </router-link>
        <span style="font-size:14px;position:absolute;right:50px">
          <span>点赞数:{{ item.likeCount }}</span><span style="padding-left:20px">浏览量:{{ item.pageViewCount }}</span><i style="margin-left:20px" class="el-icon-delete" />
          <div style="margin-top:10px">评论数:{{ item.commentCount }}</div>
        </span>
      </el-card>
    </div>

    <!-- 发帖 -->
    <el-dialog
      title="发帖"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="主题名称">
          <el-input
            v-model="form.title"
            type="textarea"
            :rows="2"
            placeholder="请输入主题"
          />
        </el-form-item>
        <el-form-item label="是否置顶">
          <el-switch v-model="form.isTop" />
        </el-form-item>
        <el-form-item style="display:flex;justify-content:flex-end;">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSubmit()">确 定</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { getPostBarList, addPostBar } from '@/api/communicate'

export default {
  name: 'CommunicateList',
  data() {
    return {
      title: '',
      dialogVisible: false,
      form: {
        title: '',
        isTop: false
      },
      data: []
    }
  },
  mounted() {
    // 数据初始化
    this.init()
  },
  methods: {
    init() {
      getPostBarList({ title: '' }).then((res) => {
        this.data = res.list
        console.log('data', this.data)
      })
    },
    // 点击发帖按钮
    add() {
      this.dialogVisible = true
    },
    // 发帖对话框提交
    onSubmit() {
      addPostBar(this.form).then(() => {
        this.$message.success({ message: '发帖成功' })
        this.form.title = ''
        this.dialogVisible = false
        this.init()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
